<?php
// +----------------------------------------------------------------------
// | HkCms 后台操作日志
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\admin\model;

use common\model\Model;

class Operationlog extends Model {

    protected $auto = ['ip', 'time'];

    /**
     * 记录日志
     * @param string $message 说明
     * @param integer $status 状态
     * @return boolean
     */
    public function record($message, $status = 0)
    {
        $fangs = 'GET';
        if (request()->isAjax()) {
            $fangs = 'Ajax';
        } else if (request()->isPost()) {
            $fangs = 'POST';
        }

        $data = array(
            'uid' => \app\admin\service\User::getInstance()->id ? : 0,
            'status' => $status,
            'info' => "提示语：{$message}<br/>模块：" . request()->module() . ",控制器：" . request()->controller() . ",方法：" . request()->action() . "<br/>请求方式：{$fangs}",
            'get' => isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'',
        );
        return $this->save($data) !== false ? true : false;
    }

    /**
     * 删除一个月前的日志
     * @return boolean
     */
    public function deleteAMonthago()
    {
        $status = $this->where("time","<", (time() - (86400 * 30)))->delete();
        
        return $status !== false ? true : false;
    }

    /**
     * 获取IP
     * @return mixed
     */
    protected function setIpAttr()
    {
        return request()->ip();
    }

    /**
     * 获取时间戳
     * @return int
     */
    protected function setTimeAttr()
    {
        return time();
    }
}
